export default class Menu {
    //菜单路径
    path: string = "";
    //菜单名称
    name: string = "";
    //子菜单
    children: Array<Menu> = [];

    constructor() {
    }

    public static convertFromRouterList(router: []): Array<Menu> {
        if (router == undefined) {
            return new Array<Menu>();
        }
        //初始化一个菜单
        let res = new Array<Menu>();
        router.forEach(item => {
            if (!item.meta?.hidden){
                res.push(Menu.convertFromRouter(item));
            }
        })
        return res;
    }

    public static convertFromRouter(router: any): Menu {
        if (router == undefined) {
            return new Menu();
        }
        //初始化一个菜单
        let res = new Menu();
        res.name = router.name;
        res.path = router.path;
        //获取孩子
        res.children = new Array<Menu>();

        let children = router.children;
        if (children != undefined) {
            children.forEach((item: { path: string; name: string; children: any[] | undefined; }) => {
                if (!item.meta?.hidden){
                    res.children.push(this.convertFromRouter(item));
                }
            });
        }
        return res;
    }
}